package com.lcy.eb.service;

import com.lcy.eb.mapper.EbProductMapper;
import com.lcy.eb.model.EbProduct;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashSet;
import java.util.Set;

@Service
public class ProductService {
    @Resource
    private EbProductMapper ebProductMapper;
    public EbProduct detail(int pid){
        EbProduct ebProduct = ebProductMapper.selectById(pid);

        return ebProduct;

    }

    public void addCar(int epId, HttpServletRequest request){
        EbProduct newEbProduct = ebProductMapper.selectById(epId);
        HttpSession session = request.getSession();
        Set<EbProduct>set=null;
        set=(Set<EbProduct>)session.getAttribute("car");
        if(set==null){
            set=new HashSet<>();
        }
        boolean flag=false;
        for(EbProduct oldEbProduct:set){
           // System.out.println(newEbProduct.getEpcId()+":"+oldEbProduct.getEpcId());
            if(newEbProduct.getEpId().equals(oldEbProduct.getEpId())){
                oldEbProduct.setCount(oldEbProduct.getCount()+1);
                System.out.println("niubi");
                flag=true;
                break;
            }
        }
        if(!flag){
            set.add(newEbProduct);
        }
        //set.add(newEbProduct);
        session.setAttribute("car",set);

    }
}
